<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the copyDirectory method from the FileUtil class, for the Dart programming language.">
  <title>copyDirectory method - FileUtil class - file_util library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false">

<div id="overlay-under-drawer"></div>

<header id="title">
  <button id="sidenav-left-toggle" type="button">&nbsp;</button>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">cli_time</a></li>
    <li><a href="../../utils_file_util/utils_file_util-library.html">file_util</a></li>
    <li><a href="../../utils_file_util/FileUtil-class.html">FileUtil</a></li>
    <li class="self-crumb">copyDirectory method</li>
  </ol>
  <div class="self-name">copyDirectory</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<main>


  <div id="dartdoc-main-content" class="main-content">
      <div>
<h1><span class="kind-method">copyDirectory</span> method 
    <a href="https://dart.dev/null-safety" class="feature feature-null-safety" title="Supports the null safety language feature.">Null safety</a>
</h1></div>

    <section class="multi-line-signature">
      

<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter">void</span>&gt;</span></span>
<span class="name ">copyDirectory</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="copyDirectory-param-sourceDirPath"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a></span> <span class="parameter-name">sourceDirPath</span>, </span></li>
<li><span class="parameter" id="copyDirectory-param-destinationDirPath"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a></span> <span class="parameter-name">destinationDirPath</span></span></li>
</ol>)

      

    </section>
    
<section class="desc markdown">
  <p>递归复制目录及其内容</p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">static Future&lt;void&gt; copyDirectory(String sourceDirPath, String destinationDirPath) async {
  try {
    &#47;&#47; 检查源目录是否存在
    final sourceDir = Directory(sourceDirPath);
    if (!await sourceDir.exists()) {
      throw FileSystemException(&#39;源目录不存在&#39;, sourceDirPath);
    }

    &#47;&#47; 创建目标目录
    final destinationDir = Directory(destinationDirPath);
    if (!await destinationDir.exists()) {
      await destinationDir.create(recursive: true);
    }

    &#47;&#47; 获取源目录中的所有实体（文件和子目录）
    final entities = await sourceDir.list(recursive: false).toList();

    &#47;&#47; 遍历并复制每个实体
    for (final entity in entities) {
      final entityType = await FileSystemEntity.type(entity.path);
      final entityName = entity.path.split(Platform.pathSeparator).last;
      final newPath = &#39;${destinationDir.path}${Platform.pathSeparator}$entityName&#39;;

      switch (entityType) {
        case FileSystemEntityType.file:
          await copyFile(entity.path, newPath);
          break;
        case FileSystemEntityType.directory:
          await copyDirectory(entity.path, newPath);
          break;
        default:
          print(&#39;跳过: ${entity.path}&#39;);
          break;
      }
    }

    print(&#39;目录复制成功: $destinationDirPath&#39;);
  } catch (e) {
    print(&#39;目录复制失败: $e&#39;);
    rethrow;
  }
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
  <li><a href="../../index.html">cli_time</a></li>
  <li><a href="../../utils_file_util/utils_file_util-library.html">file_util</a></li>
  <li><a href="../../utils_file_util/FileUtil-class.html">FileUtil</a></li>
  <li class="self-crumb">copyDirectory method</li>
</ol>


    <h5>FileUtil class</h5>
    <ol>

        <li class="section-title"><a href="../../utils_file_util/FileUtil-class.html#constructors">Constructors</a></li>
          <li><a href="../../utils_file_util/FileUtil/FileUtil.html">FileUtil</a></li>


    <li class="section-title inherited">
      <a href="../../utils_file_util/FileUtil-class.html#instance-properties">Properties</a>
    </li>
    <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/hashCode.html">hashCode</a></li>
    <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/runtimeType.html">runtimeType</a></li>

    <li class="section-title inherited"><a href="../../utils_file_util/FileUtil-class.html#instance-methods">Methods</a></li>
    <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
    <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/toString.html">toString</a></li>

    <li class="section-title inherited"><a href="../../utils_file_util/FileUtil-class.html#operators">Operators</a></li>
    <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/operator_equals.html">operator ==</a></li>



        <li class="section-title"><a href="../../utils_file_util/FileUtil-class.html#static-methods">Static methods</a></li>
          <li><a href="../../utils_file_util/FileUtil/appendText.html">appendText</a></li>
          <li><a href="../../utils_file_util/FileUtil/copyDirectory.html">copyDirectory</a></li>
          <li><a href="../../utils_file_util/FileUtil/copyFile.html">copyFile</a></li>
          <li><a href="../../utils_file_util/FileUtil/deleteDirectory.html">deleteDirectory</a></li>
          <li><a href="../../utils_file_util/FileUtil/deleteDirectoryRecursively.html">deleteDirectoryRecursively</a></li>
          <li><a href="../../utils_file_util/FileUtil/deleteEmptyDirectory.html">deleteEmptyDirectory</a></li>
          <li><a href="../../utils_file_util/FileUtil/writeText.html">writeText</a></li>

</ol>

  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    cli_time
      1.0.0
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

